<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;
use think\Cache;
use think\Db;
class Personal extends  BaseModel{

	//检测参数是否已传
	protected $ParameterDetection  = [
		'walletDetail'=>['oid'],
		'walletWithdraw'=>['passwd','money'],
		'bindcard'=>['name','card_num','bank','mobile'],
	];

	/**
	* 首页
	*/
	public function index(){
		$uid = $this->user['id'];
		$where['status']=1;
		$where['id']=$uid;
		$result = db('member')->where($where)->field('id,mobile,headimage,nickname,sex,email,is_buyer')->find();
		return $result;
	}

	/**
	* 个人中心详情
	*/
	public function userInfo(){
		$uid = $this->user['id'];
		$where['status']=1;
		$where['id']=$uid;
		$result = db('member')->where($where)->field('id,mobile,headimage,nickname,sex,email,is_buyer')->find();

		return $result;
	}

	/**
	 * 钱包余额
	 */
	public function balance()
	{
		$db_wallet = db('wallet');
		$db_withdraw = db('withdraw');
		$uid = $this->user['id'];//买手id
		$where['user_id']=$uid;

		$wallet_count = $db_wallet->where($where)->sum('price');

		$where['status'] = ['in',['0','1']];
		$withdraw_count = $db_withdraw->where($where)->sum('money');
		$balance = ($wallet_count-$withdraw_count)/100;//余额
		//手续费
		$poundage = db('config')->find();
		$result['balance'] = $balance;
		//去掉多余的0
		$poundage['poundage'] = rtrim(rtrim($poundage['poundage'], '0'), '.'); 
		$result['poundage'] = $poundage['poundage']."%";
		return $result;	
	}

	/**
	* 钱包明细
	*/
	public function wallet(){
//		die('123');
		//未到账已完成订单商品金额
		$db_wallet = db('wallet');
		$page = isset($this->requestData['page'])?$this->requestData['page']:'1';

		$uid = $this->user['id'];//买手id
		$where['user_id']=$uid;
		$list = $db_wallet->alias('w')
				->join('order_goods og', 'og.id=w.order_goods_id and og.buyer_user_id=w.user_id','INNER')
				->where($where)
				->field('w.id,price,goods_name,goods_num,goods_image,create_time')
				->order('w.id desc')
				->page($page,'10')
				->select();
		foreach($list as $key=>$value){
			$list[$key]['price'] = $list[$key]['price']/100;
			$list[$key]['goods_image'] = IMG_PATH.$list[$key]['goods_image'];
			$list[$key]['create_time'] = date("Y-m-d h:i:s", $list[$key]['create_time']);
		}

		$result['wallet'] = $list;
		return $result;
	}

	/**
	 * 提现记录
	 */
	public function withdraw()
	{
		$db_withdraw = db('withdraw');
		$page = isset($this->requestData['page'])?$this->requestData['page']:'1';

		$uid = $this->user['id'];//买手id
		$where['user_id']=$uid;

		$withdraw = $db_withdraw->where($where)->field('id,user_id,bank,card_num,money,create_time,status')->page($page,'10')->select();
		foreach($withdraw as $k=>$val){
			$withdraw[$k]['create_time'] = date('Y-m-d H:i:s',$withdraw[$k]['create_time']);
			$withdraw[$k]['money'] = $withdraw[$k]['money']/100;
		}
		$result['withdraw'] = $withdraw;
		return $result;

	}

	/**
	 *   钱包提现申请
	 */
	public function walletWithdraw(){
		$uid = $this->user['id'];

		$pass = md5($this->requestData['passwd']);
		$py_pass = db('member_buyer_check')->where('user_id='.$uid)->value('password');

		if($pass != $py_pass)return ['message'=>'支付密码错误'];

		$money = $this->requestData['money'];
		if($money < 0 || !is_numeric($money))return ['message'=>'提现金额错误'];

		$db_wallet = db('wallet');
		$where['user_id']=$uid;
		
		$wallet_count = $db_wallet->where($where)->sum('price');

		$where['status'] = ['in',['0','1']];
		$withdraw_count = db('withdraw')->where($where)->sum('money');
		$wallet = ($wallet_count-$withdraw_count)/100;//余额

		if($wallet < $money)return ['message'=>'余额不足'];

		$config_poundage = db('config')->where('id=1')->value('poundage');
		$poundage = round((floatval($config_poundage)/100)*$this->requestData['money'],2);

		$bankcard = db('bankcard')->where('user_id='.$uid)->find();
		if(empty($bankcard))return ['message'=>'请绑定银行卡！'];

		$data=[
			'name'=>$bankcard['name'],
			'card_num'=>$bankcard['card_num'],
			'bank'=>$bankcard['bank'],
			'mobile'=>$bankcard['mobile'],
			'user_id'=>$uid,
			'money'=>$money*100,
			'poundage'=>$poundage,
			'create_time'=>time(),
		];
		$db_withdraw = db('withdraw');
		Db::startTrans();
		try{
			$res=$db_withdraw->insert($data);
			if(!$res) throw new \Exception('提现失败');
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			return array("message"=>$e->getMessage());
		}
	}

	/**
	 * 绑定银行卡
	 */
	public function bindcard()
	{
		$db_bankcard = db("bankcard");
		$data = [
				'name' =>$this->requestData['name'],
				'card_num' => $this->requestData['card_num'],
				'bank' => $this->requestData['bank'],
				'mobile' =>$this->requestData['mobile'],
				"user_id"=>  $this->user['id'],
		];
		$card=$db_bankcard->where("user_id=". $this->user['id'])->find();
		if($card) return array("message"=>'无需重复绑定');
		$res=$db_bankcard->insert($data);
		if(!$res) return array("message"=>'绑定失败');
	}
	/**
	 * 银行卡详情
	 */
	public function bankdetail()
	{
		$db_bankcard = db("bankcard");
		$card=$db_bankcard->where("user_id=". $this->user['id'])->find();
		if(!$card) return array("message"=>'还没绑定银行卡');
		return $card;
	}
	/**
	 * 解绑银行卡
	 */
	public function unbundling()
	{
		$db_bankcard = db("bankcard");
		$res=$db_bankcard->where("user_id=".$this->user['id'])->delete();
		if(!$res) return array("message"=>'解绑失败');
	}

}